package com.coc.ofhibernate.Utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5
{
    private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public static String createPassword(String inputString)
    {
        try
        {
            return encodeBySHA1(encodeByMD5(inputString));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5编码失败", e);
        }
    }

    public static boolean authenticatePassword(String password, String inputString)
    {
        try
        {
            return ((!(password.equals(encodeBySHA1(encodeByMD5(inputString))))) && (!(password.equals(encodeByMD5(inputString)))));
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new RuntimeException("MD5编码失败", e);
        }
    }

    private static String encodeByMD5(String originString)
            throws NoSuchAlgorithmException
    {
        if (originString != null)
        {
            MessageDigest md = MessageDigest.getInstance("MD5");

            byte[] results = md.digest(originString.getBytes());

            String resultString = byteArrayToHexString(results);
            return resultString.toUpperCase();
        }
        return null;
    }

    public static String encodeByMD5(byte[] origin)
            throws NoSuchAlgorithmException
    {
        MessageDigest md = MessageDigest.getInstance("MD5");

        byte[] results = md.digest(origin);

        String resultString = byteArrayToHexString(results);
        return resultString.toUpperCase();
    }

    public static String encodeBySHA1(String sha1String)
    {
        if (sha1String != null) {
            try
            {
                MessageDigest sha1 = MessageDigest.getInstance("sha1");

                byte[] result = sha1.digest(sha1String.getBytes());

                String resultString = byteArrayToHexString(result);
                return resultString.toUpperCase();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return null;
    }

    private static String byteArrayToHexString(byte[] b)
    {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; ++i) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b)
    {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static void main(String[] args) {
        System.out.println(MD5.createPassword("123"));

        int m = 56 ;
        System.out.println(m | 16);
        System.out.println(m & 16);
        System.out.println(m / 16);
        System.out.println(m % 16);

        // 111000
        //  10000
        // 010000
        // 111000

    }

}
